package com.nowcoder.community.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nowcoder.community.entity.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MessageMapper extends BaseMapper<Message> {

    /**
     * 查询用户会话的消息列表, 支持分页查询.
     */
    List<UserConversation> selectConversations(int userId, int offset, int limit);

    List<SystemConversation> selectSystemConversations(int userId, int offset, int limit);

    /**
     * 查询某个会话所包含的所有私信, 支持分页查询.
     */
    List<Message> selectLetters(String conversationId, int offset, int limit);

    List<Message> selectNotice(String conversationId, int offset, int limit);

    UserConversation selectConversationById(String conversationId);

    @Select("SELECT count(from_id = 1 OR NULL) systemUnread, count(from_id != 1 OR NULL) conversationUnread " +
            "FROM message WHERE to_id = #{userId} AND status = 0"
    )
    UserMessageUnread countUserMessageUnread(int userId);

    @Select("SELECT count(*) FROM message WHERE conversation_id = #{conversationId} AND status != 2 AND from_id != 1")
    long countLettersById(String conversationId);

    long countConversationsByUserId(int userId);

    long countSystemConversationsByUserId(int userId);

    int updateStatusById(List<Integer> ids, int status);
}
